<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>分享</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        #big {
            position: absolute;
            height: 200px;
            width: 200px;
            background-color: red;
            top: 200px;
            left: -200px;
            /*transition: 2s;*/
        }

        /*sass*/
        #big #small {
            position: absolute;
            height: 50px;
            width: 50px;
            background-color: #009933;
            left: 200px;
            top: 75px;
            text-align: center;
            line-height: 50px;
        }
    </style>
</head>
<body>
<div id="big">
    <div id="small">
        分享
    </div>
</div>
</body>
<script>
    var big = document.getElementById("big");
    big.onmouseover = function () {  //数值变化  多久完成
        move(this, {"attr": "left", value: 0}, 10, 100);
    }
    big.onmouseout = function () {
        move(this, {"attr": "left", value: -200}, 10, 100);
    }

    function move(obj, json, speed, time) {
        clearInterval(obj.timer);  //清除定时器
        var attr = json.attr;
        var value = json.value;
        var currentValue = getStyle(obj, attr);
        if (value > currentValue) {  //目标值比当前值大
            speed = Math.abs(speed);
        } else {
            speed = -Math.abs(speed);
        }
        obj.timer = setInterval(function () {
            obj.style[attr] = getStyle(obj, attr) + speed + "px";
            if (speed > 0) {
                if (getStyle(obj, attr) >= value) {
                    obj.style[attr] = value + "px";
                    clearInterval(obj.timer);
                }
            } else {
                if (getStyle(obj, attr) <= value) {
                    obj.style[attr] = value + "px";
                    clearInterval(obj.timer);
                }
            }
        }, time)
    }


    function getStyle(obj, attr) {
        if (obj.currentStyle) {
            return parseInt(obj.currentStyle[attr]);
        } else {
            return parseInt(window.getComputedStyle(obj)[attr]);
        }
    }


</script>
</html>